package com.zero.system.controller;

import com.zero.system.entity.Company;
import com.zero.system.entity.Models;
import com.zero.system.service.AdminService;
import com.zero.system.service.CompanyService;
import com.zero.system.service.ModelsService;
import com.zero.system.service.RoleService;
import com.zero.system.util.AjaxResult;
import com.zero.system.util.Data;
import com.zero.system.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/manager")
public class CompanyController {

    @Autowired
    private AjaxResult ajaxResult;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private CompanyService companyService;

    /**
     * 跳转航空公司管理页面
     * @return
     */
    @GetMapping("/company")
    public String company(){
        return "manager/company/companyList";
    }

    /**
     * 异步加载航空公司列表
     * @param pageno
     * @param pagesize
     * @return
     */
    @RequestMapping("/companyList")
    @ResponseBody
    public Object companyList(@RequestParam(value = "page", defaultValue = "1") Integer pageno,
                                @RequestParam(value = "limit", defaultValue = "5") Integer pagesize,
                                String name){
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("pageno",pageno);
        paramMap.put("pagesize",pagesize);
        //判断是否为空
        if(!StringUtils.isEmpty(name)) {
            paramMap.put("name",name);
        }
        PageBean<Company> pageBean = companyService.queryPage(paramMap);
        Map<String,Object> rest = new HashMap();
        rest.put("code", 0);
        rest.put("msg", "");
        rest.put("count",pageBean.getTotalsize());
        rest.put("data", pageBean.getDatas());
        return rest;
    }

    /**
     * 跳转添加或修改航空公司页面
     * @return
     */
    @GetMapping("/addCompany")
    public String addCompany(String type, Integer id, Model model){
        if(type != null && type.equals("edit")){
            Company company = companyService.findById(id);
            model.addAttribute("company",company);
        }
        return "manager/company/addCompany";
    }

    /**
     * 添加航空公司  修改航空公司
     * @return
     */
    @PostMapping("/saveCompany")
    @ResponseBody
    public AjaxResult saveCompany(Company company){
        if(company.getId() !=null){
            //修改
            int count = companyService.update(company);
            if(count > 0){
                ajaxResult.ajaxTrue("修改成功");
            }else{
                ajaxResult.ajaxFalse("修改失败");
            }
        }else{
            //添加
            int count = companyService.add(company);
            if(count > 0){
                ajaxResult.ajaxTrue("添加成功");
            }else{
                ajaxResult.ajaxFalse("添加失败");
            }
        }
        return ajaxResult;
    }

    /**
     * 删除
     * @param data
     * @return
     */
    @PostMapping("/delCompany")
    @ResponseBody
    public AjaxResult delCompany(Data data){
        int count = companyService.delete(data.getIds());
        if(count >= data.getIds().size()){
            ajaxResult.ajaxTrue("删除成功");
        }else{
            ajaxResult.ajaxFalse("删除失败");
        }
        return ajaxResult;
    }

}
